package gdt.mvc.converters;


import gdt.dao.ClientDAO;
import gdt.entity.Client;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import org.springframework.web.jsf.FacesContextUtils;



public class ClientConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
		if(value!=null){
			ClientDAO clientDao = (ClientDAO)FacesContextUtils.getWebApplicationContext(context).getBean("clientDao");
			return clientDao.findById(Long.parseLong(value));
		}
		else
			return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
		if(value == null)
			return null;
		else
			return Long.toString(((Client)value).getId());
	}

}
